<template>
    <div class="container">
      <div class="p16">
        <el-form ref="form" :model="form" label-width="110px">
          <div class="banner">
            <div class="pt16 pb16">
              <el-button type="primary" plain @click="handleAddBanner" icon="el-icon-upload">上传文档</el-button>
            </div>
            <el-table
              :data="form.urlList"
              style="width: 100%">
              <el-table-column
                type="index"
                label="序号"
                width="50">
                </el-table-column>
              <el-table-column
                prop="title"
                label="文档标题"
                show-overflow-tooltip
                width="550">
              </el-table-column>
              <el-table-column
                prop="createTime"
                label="创建时间"
                show-overflow-tooltip
                >
              </el-table-column>
              <el-table-column
                label="操作"
                width="100">
                <template slot-scope="scope">
                  <el-link :underline="false" @click="handleDeleteRow(scope.row,scope.$index)">删除</el-link>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="save">
            <el-button type="primary" @click="handleSave">保存</el-button>
          </div>
        </el-form>
      </div>
      <el-dialog title="上传文档" :close-on-click-modal="false" :visible.sync="isShow"
        custom-class="comDialog" width="30%">
        <div class="dialogDody">
          <el-form ref="passForm"  label-width="100px" :model="itemForm" class="">
            <el-form-item label="文档标题">
              <el-input v-model="itemForm.title"></el-input>
            </el-form-item>
            <el-form-item label="上传文档">
                <el-input v-model="itemForm.name" disabled>
                    <el-upload
                        slot="append"
                        action=""
                        :http-request="(file)=>handleCustomUpload(file)"
                        :show-file-list="false"
                        >
                        <el-button icon="el-icon-upload2">选择</el-button>
                    </el-upload>
                </el-input>
            </el-form-item>
            <!-- <el-form-item label="轮播图片">
              <el-upload
                action=""
                list-type="picture-card"
                :http-request="(file)=>handleCustomUpload(file)"
                :show-file-list="false"
              >
                <div class="modelBox" v-if="itemForm.url" @click.stop>
                  <div class="model">
                    <i class="el-icon-zoom-in" @click="handlePictureCardPreview(itemForm.url)"></i>
                    <i class="el-icon-delete" @click="handleDeleteUpload"></i>
                  </div>
                  <img  :src="itemForm.url" class="el-upload--picture-card" />
                </div>
                <i slot="default" class="el-icon-plus" v-else></i>
              </el-upload>
              <div class="tips">请上传jpg、png格式，尺寸建议为900px*470px</div>
            </el-form-item> -->
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="isShow=false">取 消</el-button>
          <el-button element-loading-spinner="el-icon-loading" type="primary"
            size="mini" @click="handleBannerSave">保存</el-button>
        </span>
      </el-dialog>
      <el-image-viewer
        v-if="isShowImgView"
        :url-list="dialogImageUrl"
        :initial-index="0"
        :on-close="closeViewer"
        :z-index="3000"
      />
    </div>
  </template>
  <script>
    import {
      arraySort
    } from "@/utils";
    import naTable from "@/components/naTable";
    import {
      mapGetters
    } from "vuex";
    export default {
      name: "Materiel",
      components: {
        "el-image-viewer": () =>
        import("element-ui/packages/image/src/image-viewer"),
      },
      data() {
        return {
          form: {
          },
          urlView:[],
          dialogImageUrl:[],
          isShowImgView:false,
          rowIndex:0,
          isShow:false,
          itemForm:{
            title:'',
            subtitle:'',
            url:'',
            name:''
          },
          copyItemForm:{}
        };
      },
      computed: {
        ...mapGetters(["name"]),
      },
      created() {
      },
      mounted() {
        this.getData()
        this.copyItemForm = JSON.parse(JSON.stringify(this.itemForm))
      },
      methods: {
        getData(){
          this.$api.system.GetList(9).then((res)=>{
          this.form = res[0]
          })
        },
  
        //保存
        handleSave(){
          this.$api.system.Update(this.form).then(res=>{
            this.$message.success('保存成功！')
            this.getData()
          })
        },
  
        //预览
        handlePictureCardPreview (file) {
          this.dialogImageUrl = []
          this.dialogImageUrl.push(file)
          this.isShowImgView = true
        },
  
        //删除文件
        handleDeleteUpload(){
          this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.$api.system.Deleteblogfiles().then(()=>{

            })
            // this.itemForm.url = ''
            // this.$message({
            //   type: 'success',
            //   message: '删除成功!'
            // });
          }).catch(() => {
          });
        },
  
        //关闭预览
        closeViewer(){
          this.isShowImgView = false
        },
  
        //添加轮播图
        handleAddBanner(){
          this.itemForm = JSON.parse(JSON.stringify(this.copyItemForm))
          this.isShow = true
        },
  
        //修改
        handleEditRow(row,index){
          this.itemForm = JSON.parse(JSON.stringify(row))
          this.rowIndex = index
          this.isShow = true
        },
  
        //删除
        handleDeleteRow(row,index){
          this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            if(row.id){
                this.$api.system.Deleteblogfiles(row.id).then(()=>{
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getData()
                })
            }else{
                this.form.urlList.splice(index,1)
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            }
          }).catch(() => {
          });
        },
  
        //保存轮播图
        handleBannerSave(){
            this.form.urlList.push(this.itemForm)
            this.isShow = false
            this.$message.success('添加成功！')
        },
  
        //上传文件
        handleCustomUpload(e){
          const file = e.file;
          const type = file.name.substring(file.name.lastIndexOf(".") + 1);
          if (type !== "md") {
            this.$message.error("上传格式错误，请重新上传!");
            return false;
          } else {
              var formData = new FormData();
                formData.append("file", file);
                formData.append("output", 'json2');
                this.$api.system.upload(formData).then((res) => {
                  let prefix = '/assets'
                  this.itemForm.url = prefix+res.src
                  this.itemForm.name = res.src.substring(res.src.lastIndexOf('/')+1)
                  this.itemForm.isDocument = true
                  this.$message.success("上传成功！");
                });
          }
        },
      },
    };
  
  </script>
  <style lang="scss" scoped>
  .container::v-deep{
    $sizeWidth:148px;
    $sizeHeight:100%;
    margin-bottom: 8px;
    .save{
      margin-top: 16px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .head{
      padding-left: 16px;
      display: flex;
      align-items: center;
      height: 40px;
      background-color: rgba(253, 240, 255, 1);
      color: rgba(51, 51, 51, 1);
    }
    .tips{
      font-size:13px;
      color:rgba(153, 153, 153, 1);
    }
    .modelBox{
      .model{
        display: none;
        position: absolute;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        background-color: rgba(0, 0, 0, 0.5);
        z-index: 2;
        >*{
          font-size: 20px;
          color: #fff;
          &:hover{
            color: #3f61f1;
          }
          &:not(:last-child){
            margin-right: 8px;
          }
        }
      }
      &:hover{
        .model{
          display: flex;
          align-items: center;
          justify-content: center;
        }
      }
    }
    .el-upload{
      height: #{$sizeHeight};
      position: relative;
      .tag{
        position: absolute;
        left: 0;
        top: 0;
        padding: 4px 6px;
        line-height: 1.2em;
        background: rgba(0, 0, 0, 0.75);
        color: #fff;
        font-size: 12px;
        border-radius: 6px 0 4px 0;
        z-index: 2;
      }
    }
    .img_box{
      width: #{$sizeWidth};
      height: 150px ;
      position: relative;
      img{
        max-width: 100%;
        height: 100%;
      }
    }
    .img_boxMore{
      display: flex;
      flex-wrap: wrap;
      margin-right: -8px;
      margin-bottom: -8px;
      .img_box{
        border-radius:8px;
        border: 1px #dcdfe6 solid;
        overflow: hidden;
        margin-right: 8px;
      }
      >*{
        margin-right: 8px;
        margin-bottom: 8px;
      }
    }
  }
  
  </style>
  